﻿using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OCR.Config
{
    class LocalTemplateList_Config
    {
        public static LocalTemplateList LocalTemplateList = new LocalTemplateList();
        public static void LocalTemplateSingleInfosInitial(string name)
        {
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + "\\LocalTemplateListConfigs");
            FileInfo[] fileInfo = dir.GetFiles();
            Console.WriteLine("fileInfo.Length:  " + fileInfo.Length);

            if (fileInfo.Length == 0)
            {
                CreateDefault();
                //创建新
                name = "Default_LTL.json";
            }
            else
            {
                if (!File.Exists("LocalTemplateListConfigs\\" + LocalInfo.ProductConfigListName))
                {
                    if (!File.Exists("LocalTemplateListConfigs\\Default_LTL.json"))
                    {
                        CreateDefault();
                        //创建新
                        name = "Default_LTL.json";
                    }
                    else
                    {
                        name = "Default_LTL.json";
                    }
                }
            }


            StreamReader sr = new StreamReader("LocalTemplateListConfigs\\"+ name);
            string result = sr.ReadToEnd();
            Console.WriteLine("results:" + result);

            LocalTemplateList = JsonMapper.ToObject<LocalTemplateList>(result);

            LocalInfo.Tem_ImgPath = LocalTemplateList.TemplateImgPath;
            Console.WriteLine("LocalInfo.Tem_ImgPath: " + LocalInfo.Tem_ImgPath);

            LocalTemplateSingleInfos_Config.localTemplateSingleInfos = LocalTemplateList.LocalTemplateSingleInfos;
            LocalTemplateSingleInfos_Config.LocalTemplateSingleInfosInitial_Product();

            LocalInfo.Tem_RectMaxCount = LocalTemplateSingleInfos_Config.rectangles.Count;
            Console.WriteLine("LocalInfo.Tem_RectMaxCount: " + LocalInfo.Tem_RectMaxCount);

            int.TryParse(LocalTemplateList.CameraConfig, out int cameraConfig);
            CameraReceiveThread.SwitchUserSet((uint)cameraConfig);

            sr.Dispose();
        }
        public static void LocalTemplateInfoSave(LocalTemplateList infos)
        {
            string dateTime = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff");
            string LocalTemplateListPath = "LocalTemplateListConfigs";
            if (!Directory.Exists(LocalTemplateListPath))
            {
                Directory.CreateDirectory(LocalTemplateListPath);
            }
            string listConfigPath = LocalTemplateListPath + "\\"+ infos.ConfigName + "_LTL" +  ".json";
            File.Create(listConfigPath).Close();
            string json = JsonMapper.ToJson(infos);
            Console.WriteLine(LocalTemplateSingleInfos_Config.JsonUTF8toUnicode(json));
            File.WriteAllText(listConfigPath, LocalTemplateSingleInfos_Config.JsonUTF8toUnicode(json));
        }

        public static void CreateDefault()
        {
            List<LocalTemplateSingleInfo> localTemplateSingleInfos = new List<LocalTemplateSingleInfo>();

            List<string> infoList = new List<string>() { "2207058", "202207", "202406" };
            LocalTemplateSingleInfo localTemplateSingleInfo = new LocalTemplateSingleInfo();
            localTemplateSingleInfo.InfoList = infoList;
            localTemplateSingleInfo.rect_X = "345";
            localTemplateSingleInfo.rect_Y = "264";
            localTemplateSingleInfo.rect_Width = "148";
            localTemplateSingleInfo.rect_Height = "121";
            localTemplateSingleInfos.Add(localTemplateSingleInfo);

            LocalTemplateList localTemplateList = new LocalTemplateList();
            localTemplateList.ConfigID = "0";
            localTemplateList.ConfigName = "Default";
            localTemplateList.LocalTemplateSingleInfos = localTemplateSingleInfos;
            localTemplateList.TemplateImgPath = "TemplateInput\\Default.bmp";
            localTemplateList.CameraConfig = "0";

            LocalTemplateInfoSave(localTemplateList);
        }
    }
}
